<html>
<style type="text/css">
	textarea {
		font-family: monospace;
	}
</style>
<script src="lib/CsvToMarkdown.js"></script>
<script>
	"use strict";

	window.addEventListener('DOMContentLoaded', function () {
		var insertAtCursor = function (myField, myValue) {
			//IE support
			if (document.selection) {
				myField.focus();
				sel = document.selection.createRange();
				sel.text = myValue;
			}
			//MOZILLA/NETSCAPE support
			else if (myField.selectionStart || myField.selectionStart == '0') {
				var startPos = myField.selectionStart;
				var endPos = myField.selectionEnd;
				myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
				myField.selectionEnd = myField.selectionStart = startPos + myValue.length;
			} else {
				myField.value += myValue;
			}
		};

		var input = document.getElementById('tsv-input');
		var output = document.getElementById('table-output');

		var headerCheckbox = document.getElementById('has-headers');
		var delimiterMarker = document.getElementById('delimiter-marker');

		var getDelimiter = function () {
			var delim = delimiterMarker.value;
			if (delim == 'tab') {
				delim = "\t";
			}

			return delim;
		};

		var populateData = document.getElementById('populate-data');

		input.addEventListener('keydown', function (e) {
			if (e.key.toLowerCase() == 'tab') {
				e.preventDefault();
				insertAtCursor(e.target, "\t");
			}
		});

		var renderTable = function () {
			var value = input.value.trim();
			var hasHeader = headerCheckbox.checked;

			output.value = csvToMarkdown(value, getDelimiter(), hasHeader);
		};

		input.addEventListener('keyup', renderTable);
		headerCheckbox.addEventListener('change', renderTable);
		delimiterMarker.addEventListener('change', renderTable);

		populateData.addEventListener('change', function () {
			input.value = populateData.value.split("|").join(getDelimiter()) + "\n";
			headerCheckbox.checked = true;
			populateData.selectedIndex = 0;
			renderTable();

			if (typeof input.selectionStart != 'undefined') {
				input.select();
				input.selectionEnd = input.selectionStart = input.value.length;
			}
		});

		output.addEventListener('click', function (e) {
			e.target.select();
		});

		renderTable();
	});

</script>

<body>
	<textarea style="width: 100%; height: 200px;" id="tsv-input"></textarea>
	<label><input type="checkbox" id="has-headers" /> Use first line as headers</label>
	<select id="delimiter-marker">
	<option value="tab">Tab Separated</option>
	<option value=",">Comma Separated</option>
	<option value=";">Semicolon Separated</option>
</select>
	<select id="populate-data">
	<option>-- Populate With --</option>
	<option value="id|select_type|table|type|possible_keys|key|key_len|ref|rows|extra">MySQL EXPLAIN Headers</option>
	<option value="id|select_type|table|type|possible_keys|key|key_len|ref|rows|filtered|extra">MySQL EXPLAIN EXTENDED
		Headers
	</option>
</select>

	<hr>
	<textarea style="width: 100%; height: 200px;" id="table-output" readonly></textarea>
</body>

</html>